import hashlib
import uuid

import pandas as pd
import numpy as np
import pickle as pk
import sys

from bitarray import bitarray

from bqtool.utils.bitmap import Bitmap


class DataBitmapTest(object):

    def __init__(self, max= 10**8):
        self.max = max
        self.array = bitarray(self.max)
        self.array.setall(0)

    def add(self, dataStr):
        if not self.contains(dataStr):
            num = self.str_hash(dataStr)
            self.array[num] = 1

    def remove(self, dataStr):
        if self.contains(dataStr):
            num = self.str_hash(dataStr)
            self.array[num] = 0

    def contains(self, dataStr):
        num = self.str_hash(dataStr)
        return self.array[num]

    def bit_count(self):
        return self.array.count(1)

    def str_hash(self, dataStr):
        uuidStr = hashlib.md5(dataStr.encode()).hexdigest()
        result = ''
        for i in range(0, 8):
            sub = uuidStr[i * 4: i * 4 + 4]
            x = int(sub, 16)
            result += str(x % 10)
        return int(result)


class DataBitmapSS(object):

    def __init__(self, max= 99999989):
        self.max = max
        self.array = bitarray(self.max)

    def add(self, dataStr):
        if not self.contains(dataStr):
            num = self.str_hash(dataStr)
            self.array[num] = 1

    def remove(self, dataStr):
        if self.contains(dataStr):
            num = self.str_hash(dataStr)
            self.array[num] = 0

    def contains(self, dataStr):
        num = self.str_hash(dataStr)
        return self.array[num]

    def bit_count(self):
        return self.array.count(1)

    def str_hash(self, dataStr):
        # BKDRHash
        seed = 31
        hash = 0
        for ch in dataStr:
            hash = hash * seed + ord(ch)
        return hash % self.max
        # return (hash & 0x7FFFFFFF) >> 5
        # (int(hashlib.md5(dataStr.encode()).hexdigest(), 16) >> 8) % self.max

class tt(object):
    aa = 1


if __name__ == '__main__':

    t = tt()
    t.aa = 3
    tt.aa = 2
    print(t.aa)

    data = pd.read_csv('data1')
    bm = Bitmap()
    data['CARPLATE'].map(bm.add)
    print(bm.bit_count())


    bb = Bitmap()

    print(bb.bit_count())
    array = ['冀BXC' + str(i) for i in range(400000)]
    df = pd.DataFrame(array)
    import time
    start = time.clock()
    df[0].map(bb.add)
    end = time.clock()
    print('Running time: %s Seconds' % (end - start))
    print(bb.bit_count())





    pk.dump(bb, open("data1", "wb"))

    bb.add(10 ** 6 * 2 + 2)
    print(bb.contains(10 ** 6 * 3 + 2))
    bb.add(10 ** 6 * 3 + 2)
    bb.remove(10 ** 6 * 3 + 2)
    print(bb.contains(10 ** 6 * 3 + 2))
    pk.dump(bb, open("data1", "wb"))

    ii = 999999999999999999
    for i in range(10000):
        bb.add(ii + i)
    print(bb.bit_count())

    pk.dump(bb, open("data1", "wb"))
    print(sys.getsizeof(bb))

    bb.remove(1123000011110002)
    bb.remove(1123000011110002)
    bb.add(1123000011110002)
    print(bb.contains(1123000011110002))

    print(bb.contains(9999999999999999))
    print(bb.contains(1999999999999999))
    print('ok')
    print(bb.bit_count())
    # bb.clean(9999933999999999)
    # bb.clean(9999933999999999)
    # print(bb.bit_count())

    array = [9999999999999 for i in range(100)]
    array.append(1)
    df = pd.DataFrame(array)
    df[0].map(bb.add)
    print(bb.bit_count())
    print(bb.contains(9999933999999999))
